function validateRole()
{
	var checked = false;
	
	if(!$("#role").validate()){
		return false;
	}
	
	var count = 0;
	$('.items').each(function(){        
        checked = $(this).attr('checked');
        if(checked == true){
           count++;
        }
    });
	if(count == 0){
		alert("You must set at least 1 module");
		return false;
	}
	
	var count = 0;
	
	$('.role_name').each(function(){        
        if($("#rolename").val() == $(this).attr('value')){
           count++;
        }
    });
	if(count > 0){
		alert("Role name already existed");
		return false;
	}
	return true;
}

function validateCenterUpdate()
{
	if(!$("#center").validate()){
		return false;
	}

	var count = 0;
	var objs = $('.name');
	
	for(var i = 0; i < objs.length && !count; i++){
		for(var j = i + 1; j < objs.length; j++){	
			if($(objs[i]).val() == $(objs[j]).val()){
				count = 1;
				break;
			}
		}
	}
	if(count > 0){
		alert("Role names are duplicated");
		return false;
	}
	
	objs = $('.fullName');
	for(var i = 0; i < objs.length && !count; i++){
		for(var j = i + 1; j < objs.length; j++){
			if($(objs[i]).val() == $(objs[j]).val()){
				count = 1;
				break;
			}
		}
	}
	
	if(count > 0){
		alert("Role fullNames are duplicated");
		return false;
	}		
	
	return true;
}

function validateCreateCenter()
{
	if(!$("#new_center").validate()){
		return false;
	}

	//duplicate name validate
	var count = 0;
	var objs = $('.name');
	for(var i = 0; i < objs.length && !count; i++){		
		if($(objs[i]).val() == $("#centreacronyn").val() || objs[i].getAttribute('value') == $("#centreacronyn").val()){
			count = 1;
			break;
		}
	}
	if(count > 0){
		alert("Centre Acronyn already existed");
		return false;
	}
	
	var objs = $('.fullName');
	for(var i = 0; i < objs.length && !count; i++){		
		if($(objs[i]).val() == $("#centername").val() || objs[i].getAttribute('value') == $("#centername").val()){
			count = 1;
			break;
		}
	}
	if(count > 0){
		alert("Centre Full Name already existed");
		return false;
	}
}

function validateYearTerm()
{
	var objs2 = $(".current");
	var count = 0;
	for(var i = 0; i < objs2.length; i++){
		if($(objs2[i]).attr("checked")){
			count = 1;
			break;
		}
	}
	if((objs2.length > 0) && (count <= 0)){
		alert("Please select current year/term");
		return false;
	}
	
	return true;
}

function validateAddYearTerm()
{
	var objs = $(".add_row");
	if(objs.length > 0){
		alert("Please submit the current pending yearterm");
		return false;
	}
	
	return true;
}
function addNewYearTerm(target1, target2)
{
	if(!validateAddYearTerm()){
		return;
	}
	
	var current_year = Number($("#current_year").val());
	var current_term = Number($("#current_term").val());
	var total = Number($("#total_row").val());
	
	$.post( target1,
		{
			total: total
			, current_term: current_term
			, current_year: current_year
		}, function(data){
			$("#yearterm_table").append(data);
			$("#current_term").val(current_term + 1);
			$("#total_row").val(total + 1);
	}, "html");
	
	$("#option").html("");
	$.post( target2,
		{}, function(data){
			$("#option").html(data);
	}, "html");
}
function addNewIDType(target)
{
	var total = Number($("#total_row").val());
	
	$.post( target,
		{
			total: total
		}, function(data){
			$("#id_type_table").append(data);
			$("#total_row").val(total + 1);
			if($("#cancel").attr("disabled")){
				$("#cancel").removeAttr("disabled");
			}
	}, "html");
	
}

function deleteRow(id)
{
	var the_id = "#" + id;
	$(the_id).remove();
	
	var total = Number($("#total_row").val());
	$("#total_row").val(total - 1);
		
	if($(".new_name")){
		if($(".new_name").length <= 0){
			if(!$("#cancel").attr("disabled")){
				$("#cancel").attr("disabled", "1");
			}
		}
	}
}

function deleteYearTermRow(id)
{
	var the_id = "#" + id;
	var current_term = Number($("#current_term").val());
	var total = Number($("#total_row").val());
	
	$(the_id).remove();
	
	$("#current_term").val(current_term - 1);
	$("#total_row").val(total - 1);	
	
	$("#option").html("");
}

function changeCurrent(field)
{
	$(".current").attr('checked', false);
	$(field).attr('checked', true);
}

function migrateData(target2, year, term)
{
	var objs = $(".add_row");
	if(objs.length > 0){
		alert("Please submit the current pending yearterm");
		return false;
	}
	
	$("#option").html("");
	$.post( target2,
		{
			target_year:year,
			target_term:term
		}, function(data){
			$("#option").html(data);
	}, "html");
}

function deleteYearTermData(target, confirm_url, year, term)
{
	var message = "Are you sure delete year-term " + year + " - " + term + " and it's related data?";
	var answer = confirm (message);
	
	var password;
	// password = prompt("Password confirm");
	// var confirmed = false;

	// if(password){
		// $.post( confirm_url,
			// {
				// password:password
			// }, function(data){
				// if(data != 1){
					// alert("wrong password!");
				// } else {
					// confirmed = true;
				// }
		// }, "html");
	// }
	
	
	// if(!confirmed){
		// return;
	// }
	
	if (!answer)
		return;

	$("#option").html("");
	$.post( target,
		{year: year, term: term
		}, function(data){
			location.reload(true);
	}, "html");
}


function deleteItem(target)
{
	var message = "Are you sure delete item?";
	var answer = confirm (message);
	
	if (!answer){
		return;
	} else {
		window.location = target
	}
}